<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<jsp:include page="/includes/top.jsp"></jsp:include>
<div class="page-header">
	<h1></h1>
</div>
<!-- /.page-header -->
<div class="row">
	<div class="col-xs-12">
		<form id="uiform" class="form-horizontal">
			<div style="display: none">
				<input name="modules.moduleId" type="hidden" />
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">模块名称</label>
				<div class="col-sm-4">
					<div class="clearfix input-icon input-icon-right">
						<input name="modules.moduleName" type="text" class="form-control"
							placeholder="模块名称" />
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">模块编码</label>
				<div class="col-sm-4">
					<div class="clearfix input-icon input-icon-right">
						<input name="modules.moduleCode" type="text" class="form-control"
							placeholder="模块编码" />
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">链接路径</label>
				<div class="col-sm-4">
					<div class="clearfix input-icon input-icon-right">
						<input name="modules.modulePath" type="text" class="form-control"
							placeholder="链接路径" />
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">样式名称</label>
				<div class="col-sm-4">
					<div class="clearfix input-icon input-icon-right">
						<input name="modules.moduleClass" type="text" class="form-control"
							placeholder="样式名称" />
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">上级模块</label>
				<div class="col-sm-4">
					<div class="clearfix input-icon input-icon-right">
						<select id="mparent" name="modules.moduleParent"
							class="form-control">
						</select>
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">功能描述</label>
				<div class="col-sm-4">
					<div class="clearfix input-icon input-icon-right">
						<input name="modules.moduleDescription" type="text"
							class="form-control" placeholder="功能描述" />
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">排序</label>
				<div class="col-sm-4">
					<div class="clearfix input-icon input-icon-right">
						<input name="modules.moduleSort" type="text" class="form-control"
							placeholder="排序" value="0"/>
					</div>
				</div>
			</div>
			<div class="clearfix form-actions">
				<div class="col-md-offset-3 col-md-9">
					<button class="btn btn-info" type="button" id="save">
						<i class="icon-ok"></i> 保存
					</button>

					&nbsp; &nbsp; &nbsp;
					<button class="btn" type="reset" id="back">
						<i class="icon-undo"></i> 取消
					</button>
				</div>
			</div>
		</form>
	</div>
	<!-- /.col -->
</div>
<jsp:include page="/includes/bottom.jsp"></jsp:include>

<script src="assets/js/bootstrapValidator.min.js"></script>

<script type="text/javascript">
	var uid = getQueryString("id");
	function ininfrom() {
		var dataMap = {
			'modules.moduleId' : uid
		};
		$.ajax({
			type : "post",
			url : "getmodule",
			data : dataMap,
			dataType : "json",
			success : function(data) {
				if (typeof (data) == "object") {
					$("#uiform input").each(function() {
						var inputname = $(this).attr("name");
						if (inputname != undefined && inputname != "") {
							var colname = inputname.split('.')[1];
							$(this).val(data[colname]);
							if ($(this).attr("type") == "checkbox") {
								if (data[colname] == "1") {
									$(this).prop("checked", true);
								} else {
									$(this).prop("checked", false);
								}
							}
						}
					});
					$("#uiform select").each(function() {
						var inputname = $(this).attr("name");
						if (inputname != undefined && inputname != "") {
							var colname = inputname.split('.')[1];
							$(this).val(data[colname]);
						}
					});
				}
			}
		});
	}
	function savafrom(url) {
		$("#save").click(function() {
			$("#uiform").bootstrapValidator("validate");
			if ($("#uiform").data("bootstrapValidator").isValid()) {
				var dataMap = $("#uiform").serializeArray();
				$.ajax({
					type : "post",
					url : url,
					data : dataMap,
					dataType : "json",
					success : function(data) {
						window.open("admin/modulelist.jsp?m=" + mid, "_self");
					}
				});
			}
		});
	}
	function initselect() {
		$.ajax({
			type : "post",
			url : "moduleall",
			dataType : "json",
			success : function(data) {
				$("#mparent").append("<option value ='0'>请选择</option>");
				for (var i = 0; i < data.length; i++) {
					if(data[i].moduleId!=uid){
						$("#mparent").append("<option value ='"+data[i].moduleId+"'>"+data[i].moduleName+"</option>");
					}
				}
				if (uid != null && uid != "") {
					ininfrom();
					savafrom("editmodule");
				} else {
					savafrom("addmodule");
				}
			}
		});
	}
	$(document).ready(function() {
		initselect();
		$("#back").click(function() {
			window.history.back();
		});
		$("#uiform").bootstrapValidator({
			feedbackIcons : {
				valid : 'icon-ok-sign',
				invalid : 'icon-remove-sign',
				validating : 'icon-refresh'
			},
			fields : {
				'modules.moduleName' : {
					validators : {
						notEmpty : {
							message : '名称，不能为空！'
						}
					}
				},
				'modules.moduleSort' : {
					validators : {
						notEmpty : {
							message : '排序，不能为空！'
						},
						regexp: {
	                        regexp: /^[0-9]+$/,
	                        message: '只能输入数字'
	                    },
						greaterThan : {
							value : 0,
							inclusive : true,
							message : '最小值0'
						},
						lessThan : {
							value : 100,
							inclusive : true,
							message : '最大值100'
						}
					}
				}
			}
		});
	});
</script>